/*
Copyright [2023] [fuwen_soft]
*/
package vip.xiaonuo.modular.xylogylogs.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.util.PoiUtil;
import vip.xiaonuo.modular.xylogylogs.entity.XyLogyLogs;
import vip.xiaonuo.modular.xylogylogs.enums.XyLogyLogsExceptionEnum;
import vip.xiaonuo.modular.xylogylogs.mapper.XyLogyLogsMapper;
import vip.xiaonuo.modular.xylogylogs.param.XyLogyLogsParam;
import vip.xiaonuo.modular.xylogylogs.service.XyLogyLogsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 操作族谱日志表service接口实现类
 *
 * @author yanwei
 * @date 2023-05-05 15:44:28
 */
@Service
public class XyLogyLogsServiceImpl extends ServiceImpl<XyLogyLogsMapper, XyLogyLogs> implements XyLogyLogsService {

    @Override
    public PageResult<XyLogyLogs> page(XyLogyLogsParam xyLogyLogsParam) {
        QueryWrapper<XyLogyLogs> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(xyLogyLogsParam)) {

            // 根据用户编号 查询
            if (ObjectUtil.isNotEmpty(xyLogyLogsParam.getUserId())) {
                queryWrapper.lambda().eq(XyLogyLogs::getUserId, xyLogyLogsParam.getUserId());
            }
            // 根据族谱编号 查询
            if (ObjectUtil.isNotEmpty(xyLogyLogsParam.getLogyId())) {
                queryWrapper.lambda().eq(XyLogyLogs::getLogyId, xyLogyLogsParam.getLogyId());
            }
            // 根据操作短语 查询
            if (ObjectUtil.isNotEmpty(xyLogyLogsParam.getDictForm())) {
                queryWrapper.lambda().eq(XyLogyLogs::getDictForm, xyLogyLogsParam.getDictForm());
            }
            // 根据额外字段 查询
            if (ObjectUtil.isNotEmpty(xyLogyLogsParam.getBackF())) {
                queryWrapper.lambda().eq(XyLogyLogs::getBackF, xyLogyLogsParam.getBackF());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<XyLogyLogs> list(XyLogyLogsParam xyLogyLogsParam) {
        return this.list();
    }

    @Override
    public void add(XyLogyLogsParam xyLogyLogsParam) {
        XyLogyLogs xyLogyLogs = new XyLogyLogs();
        BeanUtil.copyProperties(xyLogyLogsParam, xyLogyLogs);
        this.save(xyLogyLogs);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<XyLogyLogsParam> xyLogyLogsParamList) {
        xyLogyLogsParamList.forEach(xyLogyLogsParam -> {
            this.removeById(xyLogyLogsParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(XyLogyLogsParam xyLogyLogsParam) {
        XyLogyLogs xyLogyLogs = this.queryXyLogyLogs(xyLogyLogsParam);
        BeanUtil.copyProperties(xyLogyLogsParam, xyLogyLogs);
        this.updateById(xyLogyLogs);
    }

    @Override
    public XyLogyLogs detail(XyLogyLogsParam xyLogyLogsParam) {
        return this.queryXyLogyLogs(xyLogyLogsParam);
    }

    /**
     * 获取操作族谱日志表
     *
     * @author yanwei
     * @date 2023-05-05 15:44:28
     */
    private XyLogyLogs queryXyLogyLogs(XyLogyLogsParam xyLogyLogsParam) {
        XyLogyLogs xyLogyLogs = this.getById(xyLogyLogsParam.getId());
        if (ObjectUtil.isNull(xyLogyLogs)) {
            throw new ServiceException(XyLogyLogsExceptionEnum.NOT_EXIST);
        }
        return xyLogyLogs;
    }

    @Override
    public void export(XyLogyLogsParam xyLogyLogsParam) {
        List<XyLogyLogs> list = this.list(xyLogyLogsParam);
        PoiUtil.exportExcelWithStream("SnowyXyLogyLogs.xls", XyLogyLogs.class, list);
    }



    @Override
    public void saveLogyLog(String userId, String logyId, String dictForm){
        save(new XyLogyLogs(userId,logyId,dictForm));
    }

}
